package com.gethistorics.components;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
 
public class Tilt {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://blog.chosun.com/web_file/blog/182/2682/2/new-google-logo-thumb.jpg");
        final BufferedImage image = ImageIO.read(url);
        final JLabel label = new JLabel(new ImageIcon(image));
        new Timer(100, new ActionListener(){
            double angle = 0;
            public void actionPerformed(ActionEvent evt) {
                angle += Math.PI/50;
                label.setIcon(new ImageIcon(tilt(image, angle)));
            }
        }).start();
        JFrame f = new JFrame("TILT");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(label);
        f.setSize(400,400);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
 
    public static BufferedImage tilt(BufferedImage image, double angle) {
        double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
        int w = image.getWidth(), h = image.getHeight();
        int neww = (int)Math.floor(w*cos+h*sin), newh = (int)Math.floor(h*cos+w*sin);
        GraphicsConfiguration gc = getDefaultConfiguration();
        BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
        Graphics2D g = result.createGraphics();
        g.translate((neww-w)/2, (newh-h)/2);
        g.rotate(angle, w/2, h/2);
        g.drawRenderedImage(image, null);
        g.dispose();
        return result;
    }
 
    public static GraphicsConfiguration getDefaultConfiguration() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        return gd.getDefaultConfiguration();
    }
}


